<?php
if ( ! isset( $content_width ) )
	$content_width = 480;

if ( function_exists('register_sidebar') )
		register_sidebars(2);



add_action( 'after_setup_theme', 'rehtse_setup' );
//admin_init();

if ( ! function_exists( 'rehtse_setup' ) ):
function rehtse_setup() {

	add_editor_style();

	add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

	add_theme_support( 'post-thumbnails' );

	add_theme_support( 'automatic-feed-links' );

	//load_theme_textdomain( 'twentyten', TEMPLATEPATH . '/languages' );

	$locale = get_locale();
	$locale_file = TEMPLATEPATH . "/languages/$locale.php";
	if ( is_readable( $locale_file ) )
		require_once( $locale_file );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => __( 'Primary Navigation', 'rehtse' ),
	) );

	// This theme allows users to set a custom background
	add_custom_background();

	// Your changeable header business starts here
	define( 'HEADER_TEXTCOLOR', '' );
	// No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
	define('HEADER_IMAGE', '%s/images/default.jpg');

	// The height and width of your custom header. You can hook into the theme's own filters to change these values.
	define('HEADER_IMAGE_WIDTH', 401);
	define('HEADER_IMAGE_HEIGHT', 285);


	// We'll be using post thumbnails for custom header images on posts and pages.
	// We want them to be 940 pixels wide by 198 pixels tall.
	// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
	set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );

	// Don't support text inside the header image.
	define( 'NO_HEADER_TEXT', true );

	// Add a way for the custom header to be styled in the admin panel that controls
	// custom headers. See twentyten_admin_header_style(), below.
	add_custom_image_header( '', 'hope_admin_header_style' );

}
endif;

if ( ! function_exists( 'hope_admin_header_style' ) ) :

function hope_admin_header_style() {
?>
<style type="text/css">
#headimg{
	background: url(<?php header_image() ?>) no-repeat;
	background-repeat: no-repeat;
	height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
	width:<?php echo HEADER_IMAGE_WIDTH; ?>px;
}

#headimg h1, #headimg #desc {
	display: none;
}
</style>
<?php
}

endif;

function header_style() {
?>
<style type="text/css">
#rotating {background: url(<?php header_image() ?>) no-repeat center;}
</style>
<?php
}

//add_custom_image_header('', 'hope_admin_header_style');


function hope_admin_menu(){
	if ( current_user_can('edit_themes') )
		add_theme_page( __( 'Theme Options'), __( 'Theme Options' ), 'edit_theme_options', 'theme-setting', 'hope_admin' );

}

function hope_flickr() {
	if ( !get_option('flickr') || get_option('ytube') )
		return;

	if( file_exists( ABSPATH . WPINC . '/rss.php') ) {
		require_once(ABSPATH . WPINC . '/rss.php');
	} else {
		require_once(ABSPATH . WPINC . '/rss-functions.php');
	}

	$items = get_option( 'rssnum' );
	if ( empty($items) || $items < 1 || $items > 50 )
		$items = 2;

	$flickr = get_option( 'flickr' );

	if ( empty( $flickr ) ){
		$flickr = "http://api.flickr.com/services/feeds/photos_public.gne?id=90827185@N00&lang=en-us&format=rss_200";
	}

	$rss = fetch_rss( $flickr );
	if( is_array( $rss->items ) ) {
		//$out = '';
		$out = wp_cache_get('hopeii');
		if ( false == $out ) {
			$items = array_slice( $rss->items, 0, $items );
			while( list( $key, $photo ) = each( $items ) ) {
				preg_match_all("/<IMG.+?SRC=[\"']([^\"']+)/si",$photo[ 'description' ],$sub,PREG_SET_ORDER);
				$photo_url = str_replace( "_m.jpg", "_m.jpg", $sub[0][1] );
				//$photo_url = str_replace( "_m.jpg", ".jpg?v=0", $sub[0][1] );

				if (strlen($out)>0)
					$out .= ",\n";

				$out .= '["' . $photo_url . '", "' . $photo['link'] . '", "_new", "'. esc_html( $photo[ 'title' ], true ). '"]';
			}

		wp_cache_set('hopeii', $out, 'theme', 21600); //cache for 6 hours
		} //end cache
	}
	?>

<script type="text/javascript">
var mygallery=new simpleGallery({
	wrapperid: "simplegallery",
	dimensions: [402, 285],
	imagearray: [
<?php
if ( !get_option('flickr') ) {?>
		["<?php header_image() ?>", "", "_new", " "],
<?php }
?>
		<?php echo $out; ?>
	],
	autoplay: [false, 2500, 2],
	persist: true,
	fadeduration: 500,
	oninit:function(){
		//Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
	},
	onslide:function(curslide, i){
		//Keyword "this": references current gallery instance
		//curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
		//i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
	}
})

</script>
<?php
}

// register settings
function register_settings_hope() {
	register_setting('hope_settings','twit');
	register_setting('hope_settings','flickr');
	register_setting('hope_settings','rssnum');
	register_setting('hope_settings','ytube');
	register_setting('hope_settings','weather');
}

add_action( 'admin_init', 'register_settings_hope' ); 

function hope_admin(){
	$twit	= esc_html( get_option('twit') );
	$flickr	= esc_html( get_option('flickr') );
	$items	= esc_html( get_option( 'rssnum' ) );
	$ytube	= esc_html( get_option( 'ytube' ) );
	if ( empty($items) || $items < 1 ) $items = 5;

	if ( isset( $_POST[ 'action' ] ) && esc_attr( $_POST[ 'action' ] ) == 'update' ) {
		if ( isset($_POST['twit']) ) {
			$twit = strip_tags(stripslashes($_POST["twit"]));
				update_option( 'twit', $twit );

		}

		if ( isset($_POST['ytube']) ) {
			$ytube = strip_tags(stripslashes($_POST["ytube"]));
				update_option( 'ytube', $ytube );

		} else {
			delete_option( 'ytube', $ytube );
		}

		if ( isset($_POST['weather']) )
			update_option( 'weather', 1 );
		else
			delete_option( 'weather' );


		if ( isset($_POST['flickr']) ) {
			$flickr = strip_tags(stripslashes($_POST["flickr"]));
			$items = strip_tags(stripslashes($_POST["rssnum"]));
				update_option( 'flickr', $flickr );
				update_option( 'rssnum', $items );
		} else {
				delete_option( 'flickr' );
				delete_option( 'rssnum' );
		}

		echo '<div id="message" class="updated fade"><p>Options saved successfully.</p></div>';
	}

?>
<div class="wrap">
<div id="icon-themes" class="icon32"><br /></div>
<h2><?php _e('Rehtse Evoli Theme Settings')?></h2>

<div id="hope-tips" class="updated fade">
<p><strong>Support The Theme</strong>"I hope to buy a notebook for my love, please feel happy to make a donation to make my dream true, thank you very much." - Patrick</p>
<div align='left'>
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=mypatricks@gmail.com&item_name=Donate%20to%20HoPE%20Theme&item_number=1244379463&amount=10.00&no_shipping=0&no_note=1&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8&return=http://patrick.bloggles.info"><img src="https://www.paypal.com/en_GB/i/btn/btn_donate_SM.gif" /></a>
</div>
<p>Add my <a href="http://facebook.com/patrickchia">facebook</a>, <a href="http://penote.com/patrick/"></a> or follow me at <a href="http://twitter.com/mypatricks">twitter</a>. Thank you.</p>
</div>

<p><strong><?php _e('These are general extras that you can enable for your entire blog themes.')?></strong></p>

<form enctype="multipart/form-data" name="form1" method="post" action="<?php echo esc_attr( str_replace( '%7E', '~', $_SERVER['REQUEST_URI'] ) ); ?>">
<?php settings_fields( 'hope_settings' ); ?>
<table class="form-table">

<tbody>
<tr>
	<th><label for="ytube"><?php _e('WeatherPress');?></label></th>
	<td><input type="checkbox" id="weather" name="weather" value="1" <?php if (get_option('weather')) echo ' checked="checked" ';?> > <label for="weather">Disabled WeatherPress </label></td>
	</tr>

<tr>
	<th><label for="twitter"><?php _e('Twitter Username');?></label></th>
	<td><input name="twit" id="twit" value="<?php echo get_option('twit');?>" size="45" type="text" class="regular-text" /></td>
</tr>
<tr>
	<th><label for="ytube"><?php _e('Video URL(Youtube)');?></label></th>
	<td><input name="ytube" id="ytube" value="<?php echo get_option('ytube');?>" size="45" type="text" class="regular-text" /></td>
	</tr>


<tr>
	<th><label for="flickr"><?php _e('flickr/PicasaWeb RSS');?></label></th>
	<td><input name="flickr" id="flickr" value="<?php echo get_option('flickr');?>" size="45" type="text" class="regular-text" /></td>
</tr>

<tr>
	<th><label for="rssnum"><?php _e('Number Photos'); ?></th>
	<td><select id="rssnum" name="rssnum"><?php for ( $i = 5; $i <= 100; ++$i ) echo "<option value='$i' ".($items==$i ? "selected='selected'" : '').">$i</option>"; ?></select></td>
</tr>
<tr>	<th><label for="help"><?php _e('Themes Help'); ?></th>
	<td><p>Note: Your RSS feed can be found on your Flickr homepage or your PicasaWeb RSS. Scroll down to the bottom of the page until you see the <em>Feed</em> link or <em>RSS</em> link and copy that into the box above.<br /></p>

<p><strong>flickr Feed Link:</strong><br />
<code>http://api.flickr.com/services/feeds/photos_public.gne?tags=rose&amp;lang=en-us&amp;format=rss_200</code><br />
<strong>Picasa RSS Link:</strong><br />
<code>http://picasaweb.google.com/data/feed/base/user/wael.tabba?alt=rss&amp;kind=album&amp;hl=en_US&amp;access=public</code>
</p>

<p class="submit">
	<input value="Save Theme Setting" type="submit" />
	<input name="action" value="update" type="hidden" /></p>
</td></form>
</tr>
</tbody>
</table>
</div>
<?php
}

function _code(){
	$ytube	= wp_specialchars( get_option('ytube') );
	$flickr	= wp_specialchars( get_option('flickr') );

	if ( $ytube ) {
		$_out = wp_oembed_get($ytube, array('width' => 402));
		$_out = preg_replace('/height=\"(.*?)\"/i', 'height="285"', $_out);

	} elseif ( $flickr ) {
		$_out = '<div id="simplegallery"></div>';
	} else {
		$_out = '<img src="'.get_header_image().'" />';
	}

	echo $_out;
}

if ( !get_option('flickr') && !isset($_POST['update']) ) {
	function hope_tips() {
		if( substr( $_SERVER[ 'PHP_SELF' ], -19 ) != '/wp-admin/index.php' )
			return;

		echo "<div id='hope-tips' class='updated fade'><p>".__('Tips: You may custom your blog theme header and gallery with your flickr.') ."</p><p>". sprintf(__('You must <a href="%1$s">enter</a> the flickr/PicasWeb feed url for it to work.'), "themes.php?page=theme-setting")."</p></div>";
	}
	add_action('admin_notices', 'hope_tips');
}

add_action( 'admin_menu', 'hope_admin_menu' );
add_action( 'wp_footer', 'hope_flickr' );

/*function weatherPress( $post_id ) {

	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
		return $post_id;

	if($post->post_type == 'revision')
		return $post_id;

	if( !current_user_can('edit_post', $post_id) )
		return $post_id;

	//get city
	$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
	$city = str_replace(' ', '+', $geoplugin['geoplugin_city']);

	//get weather
	if ( $city != '' ) {
		$geo = wp_remote_fopen('http://www.google.com/ig/api?weather='.$city .'&hl=en');
		$xmlData  = simplexml_load_string(utf8_encode($geo));

		$weather = $xmlData->weather->current_conditions->condition['data'];
	}

	if ( $weather != '' ) {
		update_post_meta($post_id, '_w', $weather.'' );
		update_post_meta($post_id, '_g', $geoplugin['geoplugin_city'] );
	}
	return $post_id;
}

function show_weather_icon() {
	global $post;
	$post_id = $post->ID;

	if ( get_option('weather') )
		return;

	$getweather = get_post_meta($post_id, '_w', true);
	$city = get_post_meta($post_id, '_g', true);

	if( $getweather != '' ) {
		$weather_icon = strtolower(str_replace(" ", "_", $getweather ));
		echo '<span class="w"><img alt="'. $getweather .'" title="'. $getweather .' at '. $city .'" src="http://www.google.com/ig/images/weather/'. $weather_icon .'.gif"></span>';
	}

	return;

}


add_action('save_post', 'weatherPress', 10, 2);
*/

?>